import store from '@/store'
import { defineStore } from 'pinia'
import { loginNet } from '@/apix/loginNet'

interface IUserState {
    token: string
}
export const useUserStore = defineStore({
    id: 'user',
    state: (): IUserState => {
      return {
        token: '',
      }
    },
    getters: {},
    actions: {
      /** 设置角色数组 */
    //   setRoles(roles: string[]) {
    //     this.roles = roles
    //   },
      /** 登录 */
      login(userInfo) {
        return loginNet(userInfo).then(res => {
          const userJson = {
            user: userInfo.name,
            time: Date.now(),
          }
          // window.localStorage.setItem('summerfarmLogin', JSON.stringify(userJson))
          window.localStorage.setItem('token', res.token)
  
          // this.getInfo()
        })
      },
      /** 获取用户详情 */
      // async getInfo() {
        
      // },
      /** 登出 */
      logout() {
        this.token = ''
        window.localStorage.setItem('token', this.token)
      },
      /** 重置 token */
      resetToken() {
        this.token = ''
        window.localStorage.setItem('token', this.token)
      },
    },
  })
  
  /** 在 setup 外使用 */
  export function useUserStoreHook() {
    return useUserStore(store)
  }